This page last changed on Nov 09, 2010 by stepheneb.

Java in the Browser

There are several options for embedding java in the browser, and then interacting with it via javascript.

Applets

Tip: Tracing and debugging applications in Java Web Start and Java Applets

Overview

Applets are fairly simple to work with.

  1. Embed an applet in the web page. Be sure to give it a DOM referencable id.
  2. Add your javascript to the page. The javascript can access all public methods of the applet's main class.
    var applet = document.getElementById("applet");
    
    function getApplet() {
    	applet = document.getElementById("applet");
    }
    
    function displayChildInfo() {
    	getApplet();
    	var child = applet.getChildOrg();
    	var disp = document.getElementById("child-info");
    	disp.innerHTML = "... Calculating child information ...";
    	disp.innerHTML = "Name: " + child.getName() + "<br/>" +
    	  "Sex: " + child.getSexAsString() + "<br/>" +
    	  "Alleles: " + child.getAlleleString() + "<br/>" +
    	  "Pheno: " + getPhenoStr(child) + "<br/>";
    }
    
    function getPhenoStr(org) {
    	var out = ""
    	var chars = org.getCharacteristics();
    	while (chars.hasMoreElements()) {
    		var c = chars.nextElement();
    		if (out != "") {
    			out += ", ";
    		}
    		out += c.getName();
    	}
    	return out;
    }
    
    function breedOrganism() {
    	getApplet();
    	applet.breedOrganism();
    }
    

Analysis

Advantages

  • Re-use existing java code, include UIs

Disadvantages

  • Still have to download (possibly large) jar files at runtime
  • Applets can have weird UI effects in the browser (flickering, refresh problems, etc.)

Unknowns

  • Can javascript access classes in the applet's classpath directly?
  • How hard will it be to serialize and save java objects?

GWT

Overview

GWT (http://code.google.com/webtoolkit/) allows developers to write AJAX websites in Java, instead of Javascript. The Java code gets cross-compiled into Javascript such that the final delivered code is HTML + Javascript, but the developer can work almost entirely in Java using their preferred IDE.

Analysis

Advantages

  • Program in Java instead of Javascript
  • Easy to hook into persistence engines and serialize/persist objects
  • Existing java libraries can be accessed via RPC calls to the server, meaning no large jar downloads
  • RPC libraries are easy to code
  • UIs are easy to code

Disadvantages

  • Cannot reuse existing UI code
  • Have to create object serialization wrappers for Java objects
  • Existing java libraries have to be accessed via RPC calls to the server, meaning possibly slow/laggy UI

Unknowns

  • Would a hybrid GWT/applet mode be advantageous?
Document generated by Confluence on Jan 27, 2014 16:56